<?php
class SearchResultsHelper extends myHelper{

	public function getItemSection($item){
		
		if($item instanceof NewItem){
			// get all Structure IDs to which new can be linked

			// 1. "Connection" association
			$structureIds = array(-1);
			foreach($item->getNewsGroup()->getStructureNewsGroup() as $rel)
					$structureIds[] = $rel['structure_id'];



			// 2. to which group it is linked to
			if($item->getNewsGroup()->getLinkToStruct())
				$structureIds[] = $item->getNewsGroup()->getLinkToStruct();


			$q = Doctrine::getTable('Structure')->createQuery()
					->from('Structure s')
					->whereIn('s.id', $structureIds)
					->andWhere('s.parameter = ?', 'section');

			StructureTable::commonActivityConstraints($q);
			
			return $q->execute()->getFirst();


		}else if($item instanceof Product){
			$structureIds = array(-1);

			$pgIds = array(-1);
			foreach($item->getProductGroups() as $rel)
					$pgIds[] = $rel['group_id'];
				

			$q = Doctrine::getTable('Structure')->createQuery()
					->from('Structure s')
					->innerJoin('s.StructureProductGroup spg')
					->whereIn('spg.group_id', $pgIds);

			StructureTable::commonActivityConstraints($q);

			return $this->getItemSection(
					$q->execute()
					->getFirst()
			);


		}else if($item instanceof Structure){
			$q = Doctrine::getTable('Structure')->createQuery()
					->from('Structure s')
					->where('s.lft <= ? and s.rgt >= ? and s.level <= ? and s.parameter = ?',
							array($item['lft'],$item['rgt'],$item['level'], 'section'));


			StructureTable::commonActivityConstraints($q);

			return $q->execute()->getFirst();
			
		}

	}
	
}